﻿// CSelectImage.cpp: 实现文件
//

#include "pch.h"
#include "Injector.h"
#include "CSelectImage.h"
#include "afxdialogex.h"
#include "CtrlCode.h"


// CSelectImage 对话框

IMPLEMENT_DYNAMIC(CSelectImage, CDialogEx)

CSelectImage::CSelectImage(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_SELECTIMAGE_DIALOG, pParent)
    , m_strImagePath(_T(""))
{

}

CSelectImage::~CSelectImage()
{
}

void CSelectImage::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_EDITBROWSE_IMAGE, m_ImageBrowseCtrl);
    DDX_Text(pDX, IDC_EDITBROWSE_IMAGE, m_strImagePath);
}


BEGIN_MESSAGE_MAP(CSelectImage, CDialogEx)
    ON_BN_CLICKED(IDC_BTN_IMAGECONFIRM, &CSelectImage::OnBnClickedBtnImageconfirm)
    ON_WM_CLOSE()
    ON_WM_DROPFILES()
END_MESSAGE_MAP()


// CSelectImage 消息处理程序

BOOL CSelectImage::OnInitDialog()
{
    CDialogEx::OnInitDialog();

    //
    //  设置打开文件对话框属性
    //

    m_ImageBrowseCtrl.EnableFileBrowseButton(_T(""), _T("EXE Files (*.exe)|*.exe||"));

    //
    //  文件拖拽初始化
    //

    DragAcceptFiles(TRUE);
    ChangeWindowMessageFilter(WM_DROPFILES, MSGFLT_ADD);
    ChangeWindowMessageFilter(SPI_SETANIMATION, MSGFLT_ADD);

    return TRUE;
}

BOOL CSelectImage::PreTranslateMessage(MSG* pMsg)
{
    if (pMsg->message == WM_KEYDOWN)
    {
        switch (pMsg->wParam)
        {
        case VK_ESCAPE:
            return TRUE;
        case VK_RETURN:
            return TRUE;
        default:
            break;
        }
    }

    return CDialogEx::PreTranslateMessage(pMsg);
}

void CSelectImage::OnBnClickedBtnImageconfirm()
{
    UpdateData(TRUE);

    if (!m_strImagePath.IsEmpty())
    {
        m_MsgImagePath = new CString(m_strImagePath);
        if (!m_MsgImagePath->IsEmpty())
        {
            ::SendMessage(this->GetParent()->m_hWnd, WM_PROCESSIMAGE, (WPARAM)m_MsgImagePath, 0);
            delete m_MsgImagePath;
        }
    }

    EndDialog(0);
}

void CSelectImage::OnClose()
{
    ::SendMessage(this->GetParent()->m_hWnd, WM_PROCESSIMAGE, 0, 0);
    CDialogEx::OnClose();
}


void CSelectImage::OnDropFiles(HDROP hDropInfo)
{
    TCHAR szFilePath[MAX_PATH];
    DragQueryFile(hDropInfo, 0, szFilePath, sizeof(szFilePath));
    SetDlgItemText(IDC_EDITBROWSE_IMAGE, szFilePath);
    DragFinish(hDropInfo);
    UpdateData(TRUE);

    CDialogEx::OnDropFiles(hDropInfo);
}
